import Goods from "./Goods";
import StorageUtils from "../btoolkit/StorageUtils";

export enum FruitType {
    Apple, Orange, Peach
}

const KEY_GAME_DATA = 'KEY_GAME_DATA';

export default class GameData {
    
    goodsList: string[];

    init() {
        //尝试读取历史存储
        let jsonStr = StorageUtils.readString(KEY_GAME_DATA);
        if (jsonStr) {
            this.goodsList = JSON.parse(jsonStr);
        }
        //初始化数据
        if (!this.goodsList) {
            this.goodsList = [];
            //默认给一个商品
            for (let i = 0; i < 1; i++) {
                let goods = new Goods();
                goods.setDataByRandom();
                this.goodsList.push(goods.toStorageData());
            }
        }
    }

    save() {
        StorageUtils.saveString(KEY_GAME_DATA, JSON.stringify(this.goodsList));
    }

}
